package com.utilmodule;

import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimesUtil {
    public static String getCurrentMiniTimes() {
        String dateStr = "";
        Date date = new Date();
        // format的格式可以任意
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            dateStr = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    public static String getCurrentSecTimes() {
        String dateStr = "";
        Date date = new Date();
        // format的格式可以任意
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            dateStr = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    public static String getTimeString(Context mContext, String times) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = simpleDateFormat.parse(times);
            SimpleDateFormat sdf = null;
            if (times.contains("-")) {
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            } else {
                sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            }
            Long timeStamp = Long.valueOf(sdf.parse(times).getTime() / 1000);
            Calendar c = Calendar.getInstance();
            c.setTimeZone(TimeZone.getTimeZone("gmt"));

            Long tsLong = c.getTimeInMillis() / 1000;
            int ts = Integer.parseInt(tsLong.toString());
            Long time = ts - timeStamp;

            if (time < 10) {
                return mContext.getString(R.string.moment_ago);
            } else if (time < 60) {
                return String.format(
                        mContext.getString(R.string.seconds_ago_string), time);
            } else if (time < 3600) {
                if (time / 60 == 1) {
                    return mContext.getString(R.string.min_ago);
                } else {
                    return String
                            .format(mContext.getString(R.string.mins_ago_string),
                                    time / 60);
                }
            } else if (time < 86400) {
                if (time / 3600 == 1) {
                    return mContext.getString(R.string.hour_ago);
                } else {
                    return String.format(
                            mContext.getString(R.string.hours_ago_string),
                            time / 3600);
                }
            } else if (time < 864000) {
                if (time / 86400 == 1) {
                    return mContext.getString(R.string.day_ago_short);
                } else {
                    return String.format(
                            mContext.getString(R.string.day_ago_string_short),
                            time / 86400);
                }
            } else {
                Date data = new Date((((long) timeStamp) * 1000));
//			String formatString = getSystemDefaultDateFormat(mContext);
                SimpleDateFormat dateFormat2 = new SimpleDateFormat(times);
                return dateFormat2.format(data);
            }

        } catch (Exception e) {

        }
        return "";
    }
}
